package com.yys.zproject.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.yys.zproject.dao.UserDao;
import com.yys.zproject.entity.UserEntity;

import java.sql.SQLException;
import java.util.List;


public class UserDaoimpl implements UserDao {
    @Override
    public void save(UserEntity user) throws SQLException {
        Entity set = Entity.create("user")
                        .set("username", user.getUsername())
                        .set("email", user.getEmail())
                        .set("password", user.getPassword());
        Db.use().insert(set);
        System.out.println("保存用户成功:" + user);

    }
    @Override
    public UserEntity getUserByUsernameAndPassword(String username, String password) {

        try
        {
            List<UserEntity> query = Db.use().query("select * from user where"+"(username = ? or email = ?) and password = ?", UserEntity.class, username, username, password);
            if (query!=null&&query.size()==1)
            {
                return query.get(0);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();

        }
        return null;

    }



}
